//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Main.h"
#include "GlassesFirms.h"
#include "GlassesBridge.h"
#include "GlassesFirmDlg.h"
#include "FormUtilities.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TfrmGlassesFirms::TfrmGlassesFirms(TComponent* Owner, TDatabase * dbMain)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);
        
   dbsMain = dbMain;
   bridge = new TGlassesBridge( dbsMain);
}
//---------------------------------------------------------------------------
void __fastcall TfrmGlassesFirms::FormDestroy(TObject *Sender)
{
   delete bridge;
}

void TfrmGlassesFirms::SetButtonsState()
{
   btnUpdate->Enabled = !tblFirms->IsEmpty();
   btnDelete->Enabled = btnUpdate->Enabled;

   if ( tblFirms->IsEmpty())
      lblName->Caption = "";
}


//---------------------------------------------------------------------------
void __fastcall TfrmGlassesFirms::btnUpdateClick(TObject *Sender)
{
   TfrmGlassesFirmDlg * frmGlassesDlg;

   frmGlassesDlg = new TfrmGlassesFirmDlg( this, bridge, tblFirms->FieldByName( "ID")->AsInteger, tblFirms->FieldByName( "NAME")->AsString, tblFirms->FieldByName( "ADDRESS")->AsString, tblFirms->FieldByName( "PHONE")->AsString);

   try
   {
      frmGlassesDlg->ShowModal();
      tblFirms->Refresh();
      SetButtonsState();
   }
   __finally
   {
      delete frmGlassesDlg;
   }
}
//---------------------------------------------------------------------------
void __fastcall TfrmGlassesFirms::btnAddClick(TObject *Sender)
{
   TfrmGlassesFirmDlg * frmGlassesDlg;

   frmGlassesDlg = new TfrmGlassesFirmDlg( this, bridge);

   try
   {
      frmGlassesDlg->ShowModal();
      tblFirms->Refresh();
      SetButtonsState();
   }
   __finally
   {
      delete frmGlassesDlg;
   }
}
//---------------------------------------------------------------------------
void __fastcall TfrmGlassesFirms::FormShow(TObject *Sender)
{
   SetButtonsState();        
}
//---------------------------------------------------------------------------
void __fastcall TfrmGlassesFirms::btnDeleteClick(TObject *Sender)
{
   if ( MessageDlg( "Are you sure ?",
                    mtWarning,
                    TMsgDlgButtons() << mbYes << mbNo,
                    0) == mrYes)
   {
      bridge->Delete( tblFirms->FieldByName( "ID")->AsInteger);
      tblFirms->Refresh();
      tblModels->Refresh();
      SetButtonsState();
   }
}
//---------------------------------------------------------------------------
void __fastcall TfrmGlassesFirms::srcFirmsDataChange(TObject *Sender,
      TField *Field)
{
   if ( !tblFirms->IsEmpty())
   {
      tblModels->Close();
      tblModels->Filter = "FIRMID = " + tblFirms->FieldByName( "ID")->AsString;
      tblModels->Open();
      tblModels->Refresh();
      lblName->Caption = tblFirms->FieldByName( "NAME")->AsString;
   }
}
//---------------------------------------------------------------------------





void __fastcall TfrmGlassesFirms::btnCloseClick(TObject *Sender)
{
   Close();        
}
//---------------------------------------------------------------------------

void __fastcall TfrmGlassesFirms::grdGlassesFirmsKeyDown(TObject *Sender,
      WORD &Key, TShiftState Shift)
{
   switch ( Key)
   {
      case VK_INSERT :
         btnAddClick( Sender);
         break;

      case VK_DELETE :
         if ( btnDelete->Enabled)
            btnDeleteClick( Sender);
         break;

      case VK_SPACE :
         if ( btnUpdate->Enabled)
            btnUpdateClick( Sender);
         break;
   }      
}
//---------------------------------------------------------------------------

